<?php
namespace app\api\controller;
/**
 * 
 */
class School 
{
    
    public function get_school(){
        $city = input('param.city','');
        //dump(23254324);exit;
        $apicode = "3697f893eac7467e92fff718a74a3f88";
        $url = "https://api.yonyoucloud.com/apis/dst/collegeInfoQuery/collegeInfoQuery"; 
        $method = "GET";
        $params = array(
                "name" => $city,
        );
        $header = array();
        $header[] = "apicode:".$apicode;
        $header[] = "content-type:application/json";
        $content = $this->linkcurl($url,$method,$params,$header);
        $result = json_decode($content,true);
        dump($result);exit;
        if($result){
            if($result['error_code']=='0'){
                print_r($result);
            }else{
                echo $result['error_code'].":".$result['reason'];
            }
        }else{
            echo "请求失败";
        }
    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function linkcurl($url,$method,$params=false,$header=false){
        $httpInfo = array();
        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_URL, $url);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         
        if (1 == strpos("$".$url, "https://"))
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     
        if($method == "POST" ){
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS, json_encode($params) );
        }else if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.http_build_query($params) );
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    // public function province(){
    //     $url = "http://119.29.166.254:9090/api/provinces";
    //     $method = "get";
    //     // $params = array(
    //     //     "id" => $pro_id,
    //     // );
    //     $header = "";
    //     $content = $this->linkcurl($url,$method,$params=false,$header=false);
    //     $result = json_decode($content,true);
    //     $re = db('city')->insertAll($result);
    //     //dump(db('member')->select());
    //     dump($result);dump($re);exit;
    // }

    public function getCities(){
        // $page = input('param.page',1);
        // $provnice = db('city')->page($page,4)->select();
        // foreach ($provnice as $key => $value) {
        //     $url = "http://119.29.166.254:9090/api/province/getCitiesByProvinceId?id=".$value['id'];
        //     $content = ihttp_get($url);
        //     $result = json_decode($content,true);
        //     if($result){
        //         foreach ($result as $key => $val) {
        //             $da = ['pid'=>$value['id'],'name'=>$val];
                    
        //             $data[] = $da;
        //             //dump($da);
        //         }  
        //     }

        // }
        // $re = db('city')->insertAll($data);
        // dump($data);dump($re);exit;
        // // $pro_id = input('param.pro_id');
        // // $url = "http://119.29.166.254:9090/api/province/getCitiesByProvinceId?id=".$pro_id;
        // // $content = ihttp_get($url);
        // // $result = json_decode($content,true);
        // //dump($result);exit;
    }

    public function getSchool(){
        $page = input('param.page',1);
        $city = db('city')->where('pid','neq',0)->page($page,4)->select();
        //dump($city);exit;

        foreach ($city as $key => $value) {
            $url="http://119.29.166.254:9090/api/university/getUniversityByCityName?name=".URLEncode($value['name']);
            $content = ihttp_get($url);
            $result = json_decode($content,true);
            if($result){
                foreach ($result as $key => $val) {
                    $da = ['cityId'=>$value['id'],'name'=>$val['name'],'website'=>$val['website'],'provinceId'=>$val['provinceId'],'level'=>$val['level'],'abbreviation'=>$val['abbreviation'],'city'=>$val['city']];
                    
                    $data[] = $da;
                    //dump($da);
                }  
            }

        }
        $re = db('school')->insertAll($data);
        dump($re);
        dump($data);

        // $name = input('param.name');
        // $url="http://119.29.166.254:9090/api/university/getUniversityByCityName?name=".URLEncode($name);
        // dump($url);//exit;
        // $content = ihttp_get($url);
        // $result = json_decode($content,true);
        // dump($result);exit;
    }

    public function getDetail(){
        $name = input('param.name');
        $url = "http://119.29.166.254:9090/api/university/getByUniversityName";
        $method = "get";
        $params = array(
            "name" => $name,
        );
        $header = "";
        $content = $this->linkcurl($url,$method,$params=false,$header=false);
        $result = json_decode($content,true);
        dump($result);exit;
    }
    
}

 
/*
1.获取省份列表： 

http://119.29.166.254:9090/api/provinces 

返回的是省份列表，其中id很重要。 

2.通过省份id查询省份城市： 

http://119.29.166.254:9090/api/province/getCitiesByProvinceId?id=省份id 

3.通过省份id查询大学： 

http://119.29.166.254:9090/api/university/getUniversityByProvinceId?id=省份id 

4.通过城市名称查询大学： 

http://119.29.166.254:9090/api/university/getUniversityByCityName?name=城市名称 

5.通过大学名称查找大学： 

http://119.29.166.254:9090/api/university/getByUniversityName?name=大学名称 
 */
?>